﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace GeneralClasses
{
    public class SchedulerPreferences
    {
        #region VARIABLES
        int prefid;
        int nursePrefWeight;
        int nurseHourWeight;
        int overallCostWeight;
        int nurseDepartmentWeight;
        int weeks;
        int hoursToRun;
        int budget;

        DatabaseConnection dbc;
        #endregion

        #region properties
        public int Budget
        {
            get { return budget; }
            set { budget = value; }
        }
        public int HoursToRun
        {
            get { return hoursToRun; }
            set { hoursToRun = value; }
        }
        public int Weeks
        {
            get { return weeks; }
            set { weeks = value; }
        }
        public int NurseDepartmentWeight
        {
            get { return nurseDepartmentWeight; }
            set { nurseDepartmentWeight = value; }
        }
        public int OverallCostWeight
        {
            get { return overallCostWeight; }
            set { overallCostWeight = value; }
        }
        public int NurseHourWeight
        {
            get { return nurseHourWeight; }
            set { nurseHourWeight = value; }
        }
        public int NursePreferenceWeight
        {
            get { return nursePrefWeight; }
            set { nursePrefWeight = value; }
        }
        public int PrefID
        {
            get { return prefid; }
            set { prefid = value; }
        }
        #endregion

        #region constructor

        public SchedulerPreferences()
        {
            dbc = new DatabaseConnection();
            prefid = -1;
            nurseDepartmentWeight = 0;
            nurseHourWeight = 0;
            nursePrefWeight = 0;
            overallCostWeight = 0;
            weeks = 0;
            hoursToRun = 0;
            budget = 0;
        }

        #endregion

        #region private

        private bool checkSchedulerPreferenceExists()
        {
            try
            {
                DataTable table = dbc.query("SELECT prefid FROM SchedulerPreference WHERE nurseprefweight='" + nursePrefWeight + "' AND nursehourweight='" + nurseHourWeight + "', AND overallcostweight='" + overallCostWeight + "', AND nursedepartmentweight='" + nurseDepartmentWeight + "'");
                if ((table != null) && (table.Rows.Count > 0))
                {
                    return true;
                }
                return false;
            }
            catch (Exception e)
            {
                UTIL.sayDebug(e);
                throw e;
            }
        }

        private int findSchedulerPrefID()
        {
            try
            {
                DataTable table = dbc.query("SELECT prefid FROM SchedulerPreference WHERE nurseprefweight='" + nursePrefWeight + "' AND nursehourweight='" + nurseHourWeight + "', AND overallcostweight='" + overallCostWeight + "', AND nursedepartmentweight='" + nurseDepartmentWeight + "'");
                if ((table != null) && (table.Rows.Count > 0))
                {
                    foreach (DataRow row in table.Rows)
                    {
                        prefid = (int)row["prefid"];
                    }
                }
                return -1;
            }
            catch (Exception e)
            {
                UTIL.sayDebug(e);
                throw e;
            }
        }

        #endregion

        #region public

        /// <summary>
        /// saves the location data to the database
        /// </summary>
        public int saveData()
        {
            try
            { //if ID ==-1
                if (this.prefid == -1)
                {
                    if (!checkSchedulerPreferenceExists())
                    {
                        //add
                        dbc.executeNonQuery("INSERT INTO SchedulerPreference (prefid,nurseprefweight,nursehourweight,overallcostweight,nursedepartmentweight,weeks,hourstorun,budget) VALUES (" + prefid + "','" + nursePrefWeight + "','" + nurseHourWeight + "','" + overallCostWeight + "','" + nurseDepartmentWeight + "','" + weeks + "','" + hoursToRun + "','" + budget);
                    }
                    return (prefid = findSchedulerPrefID());
                }
                else
                {
                    //save
                    dbc.executeNonQuery("UPDATE SchedulerPreference SET nurseprefweight='" + nursePrefWeight + ", AND nursehourweight='" + nurseHourWeight + "', AND overallcostweight='"+ overallCostWeight + "', AND nursedepartmentweight='" + nurseDepartmentWeight + "', AND weeks='" + weeks + "', AND hourstorun='" + hoursToRun + "', AND budget='" + budget +"' WHERE prefid='" + prefid + "'");
                    return prefid;
                }
            }
            catch (Exception e)
            {
                UTIL.sayDebug(e);
                throw e;
            }

        }

        public void loadContent()
        {
            try
            {
                DataTable table = dbc.query("SELECT * FROM SchedulerPreference WHERE prefid='" + prefid + "'");
                if ((table != null) && (table.Rows.Count > 0))
                {
                    foreach (DataRow row in table.Rows)
                    {
                        this.budget = (int)row["budget"];
                        this.hoursToRun = (int)row["hourstorun"];
                        this.nurseDepartmentWeight = (int)row["nursedepartmentweight"];
                        this.nurseHourWeight = (int)row["nursehourweight"];
                        this.nursePrefWeight = (int)row["nurseprefweight"];
                        this.overallCostWeight = (int)row["overallcostweight"];
                        this.prefid = (int)row["prefid"];
                        this.weeks = (int)row["weeks"];
                        break;
                    }
                }
                throw new Exception("No Rows found");
            }
            catch (Exception e)
            {
                UTIL.sayDebug(e);
                throw e;
            }

        }

        #endregion
    }
}
